package cn.yudao.org.service.impl;

import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.basic.utils.MD5Utils;
import cn.yudao.basic.utils.PageList;
import cn.yudao.basic.utils.StrUtils;
import cn.yudao.org.domain.Department;
import cn.yudao.org.domain.Employee;
import cn.yudao.org.mapper.DepartmentMapper;
import cn.yudao.org.mapper.EmployeeMapper;
import cn.yudao.org.mapper.ShopMapper;
import cn.yudao.org.query.EmployeeQuery;
import cn.yudao.org.service.IEmployeeService;
import cn.yudao.system.domain.Role;
import cn.yudao.user.domain.Logininfo;
import cn.yudao.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
* @Description: TODO 部门表的实现类
* @Author: Mr.Jia
* @Version: 1.0
* @Date:  2022/6/21 19:00
*/
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    /**
    * @Title: findAllEmployee
    * @Description: TODO 查询部门所有信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/21 18:55
    * @Parameters:
    * @Return
    */
    public List<Department> findTreeEmployee() {
        // 拿到所有的部门数据
        List<Department> allDepartment = departmentMapper.LoadAll();

        HashMap<Long, Department> longDepartmentHashMap = new HashMap<>();

        List<Department> departmentsTree = new ArrayList<>();


        // 将数据放到集合中
        for (Department department : allDepartment) {
            longDepartmentHashMap.put(department.getId(),department);
        }


//        List<Department> departments = new ArrayList<>();
        // 遍历所有的集合数据将其变成所需要的解构
        for (Department department : allDepartment) {
            if (department.getParent() !=null){
                Department departmentParent = longDepartmentHashMap.get(department.getParentId());
                if(departmentParent != null){
                    departmentParent.getChildren().add(department);
                }
            }else {
                departmentsTree.add(department);
            }
        }
            return departmentsTree;

    }

    @Override
    public List<Employee> loadbyShopId(Long shopId) {
        return employeeMapper.loadbyShopId(shopId);
    }


    /**
    * @Title: addEmployee
    * @Description: TODO 往部门表中添加数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/6/21 18:57
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void save(Employee employee) {
        if(StringUtils.isEmpty(employee.getPassword())){
            employee.setPassword("123456");
        }
        // 初始化员工对象
        initEmployee(employee);
        employeeMapper.save(employee);
        // 初始化logininfo对象
        Logininfo logininfo = new Logininfo();
        logininfo = initLoininfo(employee,logininfo);
        logininfoMapper.save(logininfo);
        // 再次修改
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.upadte(employee);
        saveEMmployeeRole(employee);

    }


    /**
    * @Title: saveEMmployeeRole
    * @Description: 新增员工和角色的关联表字段
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/13 15:16
    * @Parameters:
    * @Return
    */
    private void saveEMmployeeRole(Employee employee) {
        List<Role> role = employee.getRole();
        if (role != null){
            List<Long> longs = new ArrayList<>();
            for (Role role1 : role) {
                longs.add(role1.getId());
            }
            // 将数据重新添加到关联表
            employeeMapper.saveEmployeeRole(employee.getId(),longs);
        }
    }

    /**
    * @Title: initEmployee
    * @Description: 初始化员工表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/6 17:06
    * @Parameters:
    * @Return
    */
    private void initEmployee(Employee employee) {
        // 获得盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 使用md5加盐加密密码
        String password = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(password);
    }

    /**
    * @Title: initLoininfo
    * @Description: 初始化logininfo
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/6 17:06
    * @Parameters:
    * @Return
    */
    private Logininfo initLoininfo(Employee employee,Logininfo logininfo) {
        BeanUtils.copyProperties(employee,logininfo);
        if (employee.getState() == 0){
            logininfo.setDisable(false);
        }else {
            logininfo.setDisable(true);
        }
        logininfo.setType(0);
        logininfo.setId(employee.getLogininfoId());
        return logininfo;
    }

    @Override
    @Transactional
    public void upadte(Employee employee) {
        if (employee.getDepartmentId() != null){
            Long DepartmentId = employee.getDepartment().getId();
            // 初始化员工对象
            initEmployee(employee);
            if (DepartmentId == null){
                employee.getDepartment().setId(employee.getDepartmentId());
            }
        }
        // 初始化logininfo对象
        Logininfo logininfo = new Logininfo();
        logininfo = initLoininfo(employee,logininfo);
        // 修改
        employeeMapper.upadte(employee);
        logininfoMapper.upadte(logininfo);

        EmployeeRelationRole(employee);

    }

    /**
    * @Title: removeEmployeeRelationRole
    * @Description: 修改相关表数据
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/13 15:19
    * @Parameters:
    * @Return
    */
    private void EmployeeRelationRole(Employee employee) {
        employeeMapper.employeeRemoveRole(employee.getId());
        saveEMmployeeRole(employee);

    }

    @Override
    @Transactional
    public void removeById(Long id){
        Employee employee = employeeMapper.loadById(id);
        shopMapper.removeById(employee.getShopId());
        employeeMapper.removeById(employee.getId());
        logininfoMapper.removeById(employee.getLogininfoId());
        employeeMapper.employeeRemoveRole(employee.getId());
    }

}
